import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 任务管理系统 - 分支功能：显示任务
 * 功能：
 * 1. 显示所有任务
 * 2. 按分类显示任务
 * 3. 按优先级显示任务
 * 4. 按状态显示任务
 */
public class TaskDisplayManager {

    // 任务类
    private static class Task {
        private int id;
        private String description;
        private String status;
        private String priority;
        private String category;
        private List<String> tags;

        public Task(int id, String description, String status, String priority, String category) {
            this.id = id;
            this.description = description;
            this.status = status;
            this.priority = priority;
            this.category = category;
            this.tags = new ArrayList<>();
        }

        public int getId() {
            return id;
        }

        public String getDescription() {
            return description;
        }

        public String getStatus() {
            return status;
        }

        public String getPriority() {
            return priority;
        }

        public String getCategory() {
            return category;
        }

        public List<String> getTags() {
            return tags;
        }

        public void addTag(String tag) {
            this.tags.add(tag);
        }

        @Override
        public String toString() {
            return "Task{" +
                    "id=" + id +
                    ", description='" + description + '\'' +
                    ", status='" + status + '\'' +
                    ", priority='" + priority + '\'' +
                    ", category='" + category + '\'' +
                    ", tags=" + tags +
                    '}';
        }
    }

    // 显示管理器类
    private static class DisplayManager {
        private List<Task> tasks;
        private Map<String, List<Task>> categoryMap;
        private Map<String, List<Task>> priorityMap;
        private Map<String, List<Task>> statusMap;

        public DisplayManager(List<Task> tasks) {
            this.tasks = tasks;
            this.categoryMap = new HashMap<>();
            this.priorityMap = new HashMap<>();
            this.statusMap = new HashMap<>();
            initializeMaps();
        }

        private void initializeMaps() {
            for (Task task : tasks) {
                // 初始化分类映射
                categoryMap.computeIfAbsent(task.getCategory(), k -> new ArrayList<>()).add(task);

                // 初始化优先级映射
                priorityMap.computeIfAbsent(task.getPriority(), k -> new ArrayList<>()).add(task);

                // 初始化状态映射
                statusMap.computeIfAbsent(task.getStatus(), k -> new ArrayList<>()).add(task);
            }
        }

        public List<Task> getAllTasks() {
            return new ArrayList<>(tasks);
        }

        public List<Task> getTasksByCategory(String category) {
            return categoryMap.getOrDefault(category, new ArrayList<>());
        }

        public List<Task> getTasksByPriority(String priority) {
            return priorityMap.getOrDefault(priority, new ArrayList<>());
        }

        public List<Task> getTasksByStatus(String status) {
            return statusMap.getOrDefault(status, new ArrayList<>());
        }
    }

    // 主类
    public static void main(String[] args) {
        List<Task> tasks = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int nextId = 1;

        // 示例任务数据
        Task task1 = new Task(nextId++, "完成项目文档", "进行中", "高", "文档");
        task1.addTag("紧急");
        task1.addTag("文档");

        Task task2 = new Task(nextId++, "修复登录模块的Bug", "未完成", "中", "Bug修复");
        task2.addTag("Bug");
        task2.addTag("登录");

        Task task3 = new Task(nextId++, "设计用户界面", "完成", "高", "设计");
        task3.addTag("UI");
        task3.addTag("设计");

        tasks.add(task1);
        tasks.add(task2);
        tasks.add(task3);

        DisplayManager displayManager = new DisplayManager(tasks);

        while (true) {
            System.out.println("\n===== 任务显示系统 =====");
            System.out.println("1. 显示所有任务");
            System.out.println("2. 按分类显示任务");
            System.out.println("3. 按优先级显示任务");
            System.out.println("4. 按状态显示任务");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    displayAllTasks(displayManager);
                    break;
                case 2:
                    System.out.print("请输入分类名称: ");
                    String category = scanner.nextLine();
                    displayTasksByCategory(displayManager, category);
                    break;
                case 3:
                    System.out.print("请输入优先级 (高/中/低): ");
                    String priority = scanner.nextLine();
                    displayTasksByPriority(displayManager, priority);
                    break;
                case 4:
                    System.out.print("请输入状态 (完成/进行中/未完成): ");
                    String status = scanner.nextLine();
                    displayTasksByStatus(displayManager, status);
                    break;
                case 0:
                    System.out.println("退出系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }

    // 显示所有任务
    private static void displayAllTasks(DisplayManager displayManager) {
        List<Task> tasks = displayManager.getAllTasks();
        if (tasks.isEmpty()) {
            System.out.println("当前没有任务");
        } else {
            System.out.println("\n===== 所有任务 =====");
            for (Task task : tasks) {
                System.out.println(task);
            }
        }
    }

    // 按分类显示任务
    private static void displayTasksByCategory(DisplayManager displayManager, String category) {
        List<Task> tasks = displayManager.getTasksByCategory(category);
        if (tasks.isEmpty()) {
            System.out.println("分类 " + category + " 下没有任务");
        } else {
            System.out.println("\n===== 分类: " + category + " =====");
            for (Task task : tasks) {
                System.out.println(task);
            }
        }
    }

    // 按优先级显示任务
    private static void displayTasksByPriority(DisplayManager displayManager, String priority) {
        List<Task> tasks = displayManager.getTasksByPriority(priority);
        if (tasks.isEmpty()) {
            System.out.println("优先级 " + priority + " 下没有任务");
        } else {
            System.out.println("\n===== 优先级: " + priority + " =====");
            for (Task task : tasks) {
                System.out.println(task);
            }
        }
    }

    // 按状态显示任务
    private static void displayTasksByStatus(DisplayManager displayManager, String status) {
        List<Task> tasks = displayManager.getTasksByStatus(status);
        if (tasks.isEmpty()) {
            System.out.println("状态 " + status + " 下没有任务");
        } else {
            System.out.println("\n===== 状态: " + status + " =====");
            for (Task task : tasks) {
                System.out.println(task);
            }
        }
    }
}